package com.geilizu.dom.service.impl;

import com.geilizu.dom.dto.BuildingDto;
import com.geilizu.dom.dto.SearchDoAdminDto;
import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.entity.DormitoryAdmin;
import com.geilizu.dom.entity.Student;
import com.geilizu.dom.dao.StudentDao;
import com.geilizu.dom.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (Student)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 17:42:43
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;



    @Override
    public List<Student> queryByDormitoryId(Integer id) {
        return studentDao.queryByDormitoryId(id);

    }

    @Override
    public boolean save(Student student) {
        Date date=new Date();
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
        String time=formatter.format(date);
        student.setState("入住");
        student.setCreateDate(time);
        System.out.println(student.getState()+"   "+student.getCreateDate());
        return studentDao.save(student);
    }

    @Override
    public Map<String, Object> list(Integer page, Integer size) {
        if (page == null || size == null || page < 0) {
            page = 0;
            size = 3;
        }
        page = (page - 1) * 3;
        List<Student> studentList = studentDao.queryAll(page, size);
        Long count = studentDao.queryAllCount();
        Map<String, Object> map = new HashMap<>();
        map.put("studentList", studentList);
        map.put("count", count);
        return map;
    }

    @Override
    public Map<String, Object> search(SearchDto searchDto) {
        Integer page = (searchDto.getPage() - 1) * searchDto.getSize();
        List<Student> studentList = new ArrayList<>();
        Long count = Long.valueOf(0);
        if (searchDto.getKey().equals("number")) {
            SearchDoAdminDto searchDoAdminDto=new SearchDoAdminDto(page,searchDto.getSize(),searchDto.getValue(),"");
            studentList = studentDao.search(searchDoAdminDto);
            count = studentDao.searchCount(searchDoAdminDto);
        } else if (searchDto.getKey().equals("name")) {
            SearchDoAdminDto searchDoAdminDto=new SearchDoAdminDto(page,searchDto.getSize(),"",searchDto.getValue());
            studentList = studentDao.search(searchDoAdminDto);
            count = studentDao.searchCount(searchDoAdminDto);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("studentList", studentList);
        map.put("count", count);
        return map;
    }

    @Override
    public Integer deleteById(Integer id) {
        return studentDao.deleteById(id);
    }

    @Override
    public Integer updateById(Student student) {
        return studentDao.updateById(student);
    }

    @Override
    public Student findById(Integer id) {
        System.out.println(id);

        return studentDao.findById(id);
    }
}
